package cn.tedu.sp02.control;

import cn.tedu.entity.Item;
import cn.tedu.service.ItemService;
import cn.tedu.web.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

/**
 * @author :Colin
 * @version 创建时间: 2021/9/9 11:52
 */
@RestController
@Slf4j
public class ItemController {
    @Autowired
    private ItemService itemService;
    @GetMapping("/{orderId}")
    public JsonResult<List<Item>> getItems(@PathVariable String orderId) throws InterruptedException {
        List<Item> items = itemService.getItems(orderId);
        //随机阻塞,90%阻塞
        if(Math.random()<0.9){
            //随机延迟时长,0-5s
            int t = new Random().nextInt(5000);
            log.info("延迟: "+t);
            Thread.sleep(t);
        }
//        if(Math.random()<1){
//
//            int t = 5000;
//            log.info("延迟: "+t);
//            Thread.sleep(t);
//      }
        return JsonResult.ok().data(items);

    }
    //用postman测试,requestbody http协议体
    @PostMapping("/decreaseNumber")
    public JsonResult<?> decreaseNumber(@RequestBody List<Item> items){
        itemService.decreaseNumber(items);
        return JsonResult.ok().msg("减少库存成功");
    }
    @GetMapping("/favicon.ico")
    public void ico(){

    }

}
